<template>
  <div class="app-container">
    <el-card class="filter-container">
      <div>
        <i class="el-icon-search" />
        <span>筛选搜索</span>
        <div class="filter-btn">
          <el-button
            icon="el-icon-search"
            type="primary"
            size="small"
            @click="handleSearchList()"
          >
            查询
          </el-button>
          <el-button
            icon="el-icon-setting"
            size="small"
            @click="handleResetSearch()"
          >
            重置
          </el-button>
        </div>

      </div>
      <div style="margin-top: 25px">
        <el-form :inline="true" :model="listQuery" size="small">
          <el-row>

            <el-col :sm="12" :lg="6">
              <el-form-item label="应用名：">
                <el-input v-model="listQuery.applicationName" placeholder="应用名" clearable />
              </el-form-item>
            </el-col>

            <el-col :sm="12" :lg="6">
              <el-form-item label="IP：">
                <el-input v-model="listQuery.ip" placeholder="IP" clearable />
              </el-form-item>
            </el-col>

            <el-col :sm="12" :lg="6">
              <el-form-item label="请求地址：">
                <el-input v-model="listQuery.uri" placeholder="输入请求地址" clearable />
              </el-form-item>
            </el-col>

          </el-row>
          <el-row>
            <el-col :sm="12" :lg="12">
              <el-form-item label="请求时间：">
                <el-date-picker
                  v-model="startTime"
                  value-format="yyyy-MM-dd"
                  type="daterange"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  :default-time="['08:00:00']"
                />
              </el-form-item>
            </el-col>

            <el-col :sm="12" :lg="6">
              <el-form-item label="响应时间（秒）：">
                <el-slider
                  v-model="responseTime"
                  range
                  show-stops
                  :max="600"
                />
              </el-form-item>
            </el-col>

          </el-row>

        </el-form>
      </div>
    </el-card>

    <el-card class="table-container">
      <el-table
        v-loading="listLoading"
        element-loading-text="加载中"
        :data="list"
      >
        <el-table-column align="center" label="用户id" property="userId" />
        <el-table-column align="center" fixed label="应用名" property="applicationName" />
        <el-table-column align="center" fixed label="请求地址" property="uri" />
        <el-table-column align="center" label="请求方法" property="method" />
        <el-table-column align="center" label="UserAgent" property="userAgent" />
        <el-table-column align="center" label="IP" property="ip" />
        <el-table-column align="center" label="开始时间" property="startTime" />
        <el-table-column align="center" label="请求时间（毫秒）" property="responseTime" />
        <el-table-column align="center" label="CODE" property="resultCode" />
        <el-table-column align="center" label="resultMessage" property="resultMessage" />

      </el-table>

      <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageLimit" @pagination="getList" />
    </el-card>

  </div>
</template>

<script>

import {
  getList
} from '@/api/log/access-log'

import Pagination from '@/components/Pagination'

const defaultListQuery = {
  pageNum: 1,
  pageLimit: 10,
  sorts: '-startTime'
}

export default {
  components: { Pagination },

  data() {
    return {
      responseTime: null,

      startTime: null,

      listQuery: Object.assign({}, defaultListQuery),
      list: [],
      total: 0,

      listLoading: true,
      loading: false
    }
  },
  watch: {
    startTime: function(newValue) {
      if (newValue != null) {
        this.listQuery.startTimeOfStart = newValue[0]
        this.listQuery.startTimeOfEnd = newValue[1]
      } else {
        this.listQuery.startTimeOfStart = null
        this.listQuery.startTimeOfEnd = null
      }
    },
    responseTime: function(newValue) {
      if (newValue != null) {
        this.listQuery.responseTimeMin = newValue[0] * 1000
        this.listQuery.responseTimeMax = newValue[1] * 1000
      } else {
        this.listQuery.responseTimeMin = null
        this.listQuery.startTimeOfEnd = null
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      getList(this.listQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleSearchList() {
      this.listQuery.pageNum = 1
      this.getList()
    },
    handleResetSearch() {
      this.listQuery = Object.assign({}, defaultListQuery)
    }
  }
}
</script>

<style scoped>
</style>
